package com.lumlord.service;

import com.lumlord.common.cache.CacheCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 缓存配置文件
 */
@Component
public class LumlordCacheServer {


    /**
     * 是否在 redis 缓存中
     * @return
     */
    public boolean hasCache(String cacheKey,Class clazz){
        return  redisTemplate.hasKey(CacheCenter.$(cacheKey,clazz));
    }

    /**
     * 是否在 redis 缓存中
     * @return
     */
    public boolean delCache(String cacheKey,Class clazz){
        String key = CacheCenter.$(cacheKey,clazz);
        return  redisTemplate.delete(key);
    }


    /**
     * 是否在 redis 缓存中
     * @return
     */
    public Object cache(String cacheKey,Class clazz){
         return redisTemplate.opsForValue().get(CacheCenter.$(cacheKey,clazz));
    }

    /**
     * 加入 redis 缓存中
     * @param  millisSeconds 毫秒数
     * @return
     */
    public void cache(String cacheKey,Class clazz,Object val,int millisSeconds){
          redisTemplate.opsForValue().set(CacheCenter.$(cacheKey,clazz),val, millisSeconds,TimeUnit.MILLISECONDS);
    }




    @Autowired
    private RedisTemplate redisTemplate;
}
